package my.service;

import org.springframework.context.support.AbstractMessageSource;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.Map;
import java.util.WeakHashMap;

/**
 * 从数据库中查message.
 * @author Hubert
 */
public class RepositoryMessageSource extends AbstractMessageSource {
    private final Map<String, MessageFormat> cachedMessageFormats = new WeakHashMap<>();

    @Override
    protected String resolveCodeWithoutArguments(String code, Locale locale) {
        return "";
    }



    @Override
    protected MessageFormat resolveCode(String code, Locale locale) {
        String key = code + '_' + locale.toString();
        String msg = findMsg(key, locale);
        if (msg == null) {
            return null;
        }
        MessageFormat messageFormat = this.cachedMessageFormats.get(key);
        if (messageFormat == null) {
            messageFormat = createMessageFormat(msg, locale);
            this.cachedMessageFormats.put(key, messageFormat);
        }
        return messageFormat;
    }



    public String findMsg(String pMsgKey, Locale locale) {
        // sqlDsl
        return "";
    }



    public void clearCache() {
        cachedMessageFormats.clear();
    }
}
